<?php

if (isset($_SERVER["HTTP_HOST"])){
	die('Access only over CLI');	
}

include "_lib.php";

$x = SQL_select_one("vote", "date = date(now())");
if ($x <> null)
	die('Maximal ein update pro Tag');
	
$count = 0;
	
$set = SQL_select_as_array("vote_catch", false , "count(*) as count, id", False, "id");
if ($set <> null){
	$sql = "INSERT INTO vote (id, count, date) VALUES ";
	while( list ( $key, $val ) = each ( $set ) ){
		$sql .= "({$val["id"]},{$val["count"]},date(now())),";
		$count += $val["count"];
	}
	$sql = substr($sql,0,-1).";";
	
	if (SQL_query($sql)){
		SQL_query("TRUNCATE `vote_catch`");
		echo "$count Stuemmen wurden abgegeben\r\n";
	}
	
	$set = SQL_select_as_array("vote", false , "count(*) as count, id", False, "id");
	while( list ( $key, $val ) = each ( $set ) ){
		SQL_update("server", "id = ".$val["id"], "count = ".$val["count"]);
	}
}